﻿<%@ Page Language="C#" AutoEventWireup="true" Inherits="System.Web.UI.Page" %>

<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Data" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
	<title>驾驶技术网 | 搜索</title>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<link href="/Styles/Site.css" rel="stylesheet" type="text/css" />
	<script src="/Scripts/jquery.min.161.js" type="text/javascript"></script>
	<script src="/Scripts/jquery.simplemodal.1.4.3.min.js" type="text/javascript"></script>
	<script src="/Scripts/comm.js" type="text/javascript"></script>
	<script src="/Scripts/site.js" type="text/javascript"></script>
	<!-- 板块js -->
	<script src="/Templates/parts.js" type="text/javascript"></script>
	<style type="text/css">
		.list { width:100%; }
		.list th { border-bottom: dotted 1px #aaa; text-align:left; }
		.list td, .list th { padding:3px 0px 1px 16px; }
		.list tr:nth-child(even) td { background-color: #f6f6f6; }
		#pager { border-top: dotted 1px #aaa; text-align:left; }
	</style>
</head>

<body>
	<form id="form1" class="page" runat="server">
	<%-- 页码信息 --%>
	<input type="hidden" id="htmPagerows" value="30" runat="server" /> <%-- 每页行数，从页面传到服务器 --%>
	<input type="hidden" id="htmPages" value="" runat="server" /> <%-- 页数，从服务器端初始化 --%>
	<input type="hidden" id="htmPage" value="" runat="server" /> <%-- 当前页，从服务器端初始化 --%>
	<%-- 页眉板块 --%>
	<script type="text/javascript">		document.write(jsonparts["10"]);</script>
	<!-- left menu -->
	<div class="pageleft">
		<!-- 热点推荐 -->
		<script type="text/javascript">			document.write(jsonparts["13"]);</script>
		<div class="separation10"></div>
		<script type="text/javascript"><!--
			google_ad_client = "pub-2733994180506707";
			/* 160x600, 创建于 10-6-3 */
			google_ad_slot = "6252507617";
			google_ad_width = 160;
			google_ad_height = 600;
		//-->
		</script>
		<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
	</div>
    <div class="pageright">
	<!-- 搜索 -->
	<%--<div id="htmsearch"></div>
	<script type="text/javascript">$("#htmsearch").html(jsonparts["17"]);</script>
	<script type="text/javascript">$("#htmsearch")[0].innerHTML=jsonparts["17"];</script>--%>
	<script type="text/javascript">document.write(jsonparts["17"]);</script>
		<div class="separation10"></div>
		<script type="text/javascript"><!--
			google_ad_client = "pub-2733994180506707";
			/* 160x600, 创建于 10-6-3 */
			google_ad_slot = "6252507617";
			google_ad_width = 160;
			google_ad_height = 600;
		//-->
		</script>
		<script type="text/javascript"
		src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
		</script>
	</div>
	
	<div class="column650">
		<div class="combox">
			<h5><a href="/">首页</a> &gt;&gt; 搜索　<span style="font-weight:normal">共<%=count %>项，共<%=pages %>页</span></h5>
	<table class="list" cellpadding="0" cellspacing="0">
		<%
			StringBuilder str = new StringBuilder(10000);
			int n = 0;
			// 文章列表
			str.Append("<tr><th class='w150'>发布日期</th><th>文章标题</th>");
			foreach (System.Data.DataRow row in articleRows)
			{
				str.AppendFormat("<tr><td>{0}</td><td><a href='/Articles/{1}-{2}.htm'>{3}</td></tr>", row["artUpdateTime"], row["acidColumnID"], row["artID"], row["artTitle"]);
				n++;
			}
			for (int i = n; i < pgrows; i++)	// 行数较少时补空行
			{
				str.Append("<tr><td>&nbsp;</td><td>&nbsp;</td></tr>");
			}

			Response.Write( str );
		%>

	</table>
	<div class="separation10"></div>
	<div id="pager"></div>
	<div class="separation10"></div>
	<script type="text/javascript">
		HPages(
	{ ctrlid: "#pager",
		page: $("#htmPage").val(), // 要显示的页号，从0开始。
		pages: $("#htmPages").val(),
		pagenu: 13, // 底部显示13个页号
		gopage: function (pa) {
			window.location = "t8.aspx?page=" + pa;
		}
	});

	</script>
		</div>
	</div>
	<div class="separation10"></div>		
	<!-- 页脚 -->
	<script type="text/javascript">		document.write(jsonparts["15"]);</script>
	</form>
</body>
</html>


<script runat="server">
	protected hcms.Admin.Model model = hcms.Global.model;	// 方法集合
	protected DataRowCollection articleRows;	// 文章集合
	protected int pgrows;						// 每页行数
	int count;									// 总项数
	protected int pages = 0;					// 页数
	protected void Page_Load(object sender, EventArgs e)
	{
		pgrows = int.Parse(htmPagerows.Value);
		int page = 1;
		try { page = int.Parse(Request["page"]) + 1; }
		catch { }	// page，取1

		int code = 0;
		try { code = int.Parse(Request.Cookies["searchcode"].Value); }
		catch { }	// 参数code不是整数时，取0

		string search = Request.Cookies["searchtext"] == null ? "" : Request.Cookies["searchtext"].Value;
		search = HttpUtility.UrlDecode(search);	// 解码
		search = HttpUtility.UrlDecode(search);	// ?? 不知道为什么要解两次
		articleRows = getSearchArticles(code, search, ref page, pgrows, ref pages, ref count);	// [in][out]page 页号从1开始

		htmPage.Value = (page - 1).ToString();	// htmPage 页号，从0开始
		htmPages.Value = pages.ToString();
	}

	/// <summary>取指定栏目ID的文章表，分页</summary>
	/// <param name="code">搜索条件掩码，二进制低四位有效，0 is false, 1 is true, from bit 1 to bit 4 分别表示：内容、标题、论坛、文章</param>
	/// <param name="search">搜索文字</param>
	/// <param name="page">[in][out]页号，从1开始, 返回正确页号</param>
	/// <param name="pagerows">每页行</param>
	/// <param name="pages">返回页数</param>
	/// <returns>行集合, 按级别和发布日期降序排列</returns>
	public DataRowCollection getSearchArticles(int code, string search, ref int page, int pagerows, ref int pages, ref int count)
	{
		// 如果没选范围时，算全选
		//if ((code & 0xC) == 0)
		//	code |= 0xC;
		if ((code & 0x3) == 0)
			code |= 0x3;

		StringBuilder where1 = new StringBuilder(100);
		if ((code & 0xC) == 0x8)	// 只有文章
			where1.Append("AND (acidColumnID NOT LIKE '010%')");	// 排除论坛
		else if ((code & 0xC) == 0x4)	// 只有论坛
			where1.Append("AND (acidColumnID LIKE '010%')");	// 只选论坛
		
		StringBuilder where2 = new StringBuilder(200);
		if ((code & 0x2) != 0)	// 搜索标题
			where2.AppendFormat("(a.artTitle LIKE '%{0}%')", search);
		if ((code & 0x3) == 0x3)	// 搜索标题和内容
			where2.Append(" OR ");
		if ((code & 1) != 0)	// 搜索内容
			where2.AppendFormat("(a.artContent LIKE '%{0}%')", search);
		
		string strsql = string.Format(
			"SELECT COUNT(*) AS cnt " +
			"FROM hcms_Articles AS a INNER JOIN " +
				"(SELECT acidArticleID, acidColumnID " +
				"FROM hcms_ArticleIDandColumnID " +
				"WHERE (acidDeleted = 0) {0}) AS b ON a.artID = b.acidArticleID " +
			"WHERE {1}",
			where1,
			where2
		);
		count = (int)model.da.ExecuteScalar(strsql);
		pages = (count + pagerows - 1) / pagerows;

		if (page > pages) page = pages;
		else if (page <= 0) page = 1;

		strsql = string.Format(
			"SELECT * " +
			"FROM (SELECT a.*, b.acidColumnID, ROW_NUMBER() OVER (ORDER BY a.artUpdateTime DESC) AS n " +
				"FROM hcms_Articles AS a INNER JOIN " +
					"(SELECT acidArticleID, acidColumnID " +
					"FROM hcms_ArticleIDandColumnID " +
					"WHERE (acidDeleted = 0) {0}) AS b ON a.artID = b.acidArticleID " +
				"WHERE {1}) AS c " +
			"WHERE n > {2} AND n <= {3}",	// 分页
			where1,
			where2,
			(page - 1) * pagerows,
			page * pagerows
		);
		DataTable dt = model.da.GetTable(strsql);
		return dt.Rows;
	}

</script>
